import {createRouter, createWebHistory} from "vue-router";

import {getLoginStatus} from "@/api/LoginRequest.js";

import Page404 from "@/components/404.vue";
import Home from "@/views/Home.vue";
import DpalyerDemo from "@/components/demos/DplayerDemo.vue";
import UploadDemo from "@/components/demos/UploadFileDemo.vue";
import ArtPlayerDemo from "@/components/demos/ArtPlayerDemo.vue";
import login from "@/views/user/Login.vue";
import PlayVideo from "@/views/Player.vue";
import Publish from "@/views/uploadManager/PublishCenter.vue";
import uploadVideo from "@/views/uploadManager/upload-video.vue";
import uploadArticle from "@/views/uploadManager/upload-article.vue";
import manageVideo from "@/views/uploadManager/manager-video.vue";
import manageArticle from "@/views/uploadManager/manager-article.vue";
import Register from "@/views/user/Register.vue";
import MessageCenter from "@/views/message/message-center.vue";
import MessageOwn from "@/views/message/message-own.vue";
import MessageLike from "@/views/message/message-like.vue";
import MessageReply from "@/views/message/message-reply.vue";
import SearchMain from "@/views/search/search-main.vue";
import SearchVideo from "@/views/search/search-video.vue";
import SearchUser from "@/views/search/search-user.vue";
import UserCenter from "@/views/user/UserCenter.vue";

//配置导出路由
const routes = [
    {path: '/:pathMatch(.*)*', component: Page404, meta: {title: '404'}},
    {path: '/', component: Home, meta: {title: '首页'}},
    {path: '/video/:videoId', component: PlayVideo, name: 'playVideo', meta: {title: '播放页面'}},
    {path: '/upload-manager', component: Publish, meta: {title: '创作中心', requireAuth: true},
        children: [
            {path: 'upload-video', component: uploadVideo, meta: {requireAuth: true}},
            {path: 'upload-article', component: uploadArticle, meta: {requireAuth: true}},
            {path: 'manager-video', component: manageVideo, meta: {requireAuth: true}},
            {path: 'manager-article', component: manageArticle, meta: {requireAuth: true}}
        ]
    },
    {path: '/message-center', component: MessageCenter, meta: {title: '消息中心', requireAuth: true},
        children: [
            {path: 'own', component: MessageOwn, meta: {requireAuth: true}},
            {path: 'like', component: MessageLike, meta: {requireAuth: true}},
            {path: 'reply', component: MessageReply, meta: {requireAuth: true}}
        ]
    },
    {path: '/search', component: SearchMain, meta: {title: '搜索'},
        children: [
            {path: 'video', component: SearchVideo},
            {path: 'user', component: SearchUser},
        ]
    },
    {path: '/login', component: login, meta: {title: '登录'}},
    {path: '/register', component: Register, meta: {title: '注册'}},
    {path: '/demo/dplayer', component: DpalyerDemo, meta: {title: 'dplayer', requireAuth: true}},
    {path: '/demo/art-player', component: ArtPlayerDemo, meta: {title: 'art-player'}},
    {path: '/demo/upload', component: UploadDemo, meta: {title: '上传文件'}},
    {path: '/user-center/:userId', component: UserCenter, name: 'userCenter', meta: {title: '用户中心'}},
]

const router = createRouter({
    base: 'app',
    history: createWebHistory(),
    routes
})

/**
 * 路由错误回调
 */
router.onError((err, vm, info) => {
    console.log(`组件${vm.$vnode.tag}发生错误：${err.message},${info}`);
});


/**
 * 路由前置守卫
 */
router.beforeEach(async (to, from, next) => {
    window.document.title = to.meta.title === undefined ? '默认标题' : to.meta.title
    // 请求登录状态
    const login_status = await getLoginStatus();
    if (to.meta.requireAuth && !login_status.data) {
        // 跳转到登录页
        next('/login');
    } else {
        next();
    }
})

export default router